package com.hua.ui;

import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Image;
import java.sql.SQLException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.hua.Dao.SqlService;
import com.hua.Model.UserInfo;
import com.hua.Service.funtion;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;

public class register extends JPanel {
	private JTextField textField;
	private int width = 300, height = 300;
	private ImageIcon image;
	private JPasswordField passwordField;
	private JPasswordField passwordField_1;

	/**
	 * Create the panel.
	 */
	public register() {
		setLayout(null);
		JLabel lblNewLabel = new JLabel("欢迎注册成为花的用户");
		lblNewLabel.setFont(new Font("Alimama ShuHeiTi", Font.BOLD, 24));
		lblNewLabel.setBounds(420, 23, 270, 69);
		add(lblNewLabel);

		JTextField jt2 = new JTextField();
		jt2.setBounds(464, 107, 260, 33);
		add(jt2);
		jt2.setColumns(10);

		JButton btnNewButton = new JButton("下一步");
		btnNewButton.setBounds(598, 339, 126, 46);
		add(btnNewButton);

		JLabel lblNewLabel_1 = new JLabel("名称：");
		lblNewLabel_1.setFont(new Font("Alimama ShuHeiTi", Font.BOLD, 17));
		lblNewLabel_1.setBounds(365, 106, 79, 33);
		add(lblNewLabel_1);

		JLabel lblNewLabel_1_1 = new JLabel("账户名：");
		lblNewLabel_1_1.setFont(new Font("Alimama ShuHeiTi", Font.BOLD, 17));
		lblNewLabel_1_1.setBounds(365, 152, 79, 33);
		add(lblNewLabel_1_1);

		textField = new JTextField();
		textField.setColumns(10);
		textField.setBounds(464, 153, 260, 33);
		add(textField);

		JLabel lblNewLabel_1_1_1 = new JLabel("密码：");
		lblNewLabel_1_1_1.setFont(new Font("Alimama ShuHeiTi", Font.BOLD, 17));
		lblNewLabel_1_1_1.setBounds(365, 198, 79, 33);
		add(lblNewLabel_1_1_1);

		JLabel lblNewLabel_1_1_1_1 = new JLabel("确认密码：");
		lblNewLabel_1_1_1_1.setFont(new Font("Alimama ShuHeiTi", Font.BOLD, 17));
		lblNewLabel_1_1_1_1.setBounds(365, 244, 97, 33);
		add(lblNewLabel_1_1_1_1);

		JLabel lblNewLabel_2 = new JLabel("");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.LEFT);
		image = new ImageIcon("src/main/resources/images/webdisk2.png");
		// image.setImage(image.getImage().getScaledInstance(width,
		// height,Image.SCALE_DEFAULT));
		Image img = image.getImage();
		img = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
		image.setImage(img);
		lblNewLabel_2.setIcon(image);
		lblNewLabel_2.setBounds(14, 62, 316, 303);
		add(lblNewLabel_2);
		// 密码输入框
		passwordField = new JPasswordField();
		passwordField.setBounds(464, 199, 260, 32);
		add(passwordField);

		passwordField_1 = new JPasswordField();
		passwordField_1.setBounds(464, 249, 260, 32);
		add(passwordField_1);

		JCheckBox chckbxNewCheckBox = new JCheckBox("我已认真阅读并同意《不隐私政策》");
		chckbxNewCheckBox.setFont(new Font("Alimama ShuHeiTi", Font.PLAIN, 15));
		chckbxNewCheckBox.setSelected(true);
		chckbxNewCheckBox.setBounds(365, 286, 361, 44);
		add(chckbxNewCheckBox);
		// 添加点击事件监听器
		btnNewButton.addActionListener(e -> {
			if (chckbxNewCheckBox.isSelected()) {
			// 获取相关数据
			String name = jt2.getText();
			String account = textField.getText();
			String password = new String(passwordField.getPassword());
			String real_password = new String(passwordField_1.getPassword());
			funtion fun = new funtion();
			if (account.isEmpty() || password.isEmpty() || !funtion.check_str(account, "账户") || funtion.check_str(account, "含中文") || account.length() >30 || account.length() <11 || password.length() < 6) {
				System.out.println(funtion.check_str(account, "账户"));
				funtion.tips("提示", "账户名或密码异常(需要账户名11位，密码6位以上)");
				// System.out.println("账户名或密码不完整");
			} else {
				// 注册逻辑
				SqlService Service = new SqlService();
				if (!fun.is_account(account)) { // 检查账户名是否被注册
					if (!password.equals(real_password)) {
						funtion.tips("密码", "两次输入的密码不相同!");
					} else {
						// 传入加密SHA256函数加密
						String en_password = fun.encryptSHA256(password);
						int user_count = 0;
						try {
							// 调用注册函数
							UserInfo user = new UserInfo();
							user.register_user(name, account, en_password);
							user_count = Service.register_user(user);
							String stat_bulid = fun.build_floder(account, en_password);
							// 抛出结果
							if (user_count == 0 || stat_bulid.equals("失败")) {
								funtion.tips("注册失败", "注册失败！");
							} else {
								funtion.tips("注册成功", "注册成功！请登录!");
							}
						} catch (SQLException e1) {
							// 抛出异常
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				} else {
					funtion.wanr_tips("注册用户", "该账户已被注册，请登录!\n忘记密码请联系管理员");
				}
			}
			} else {
				funtion.wanr_tips("协议同意", "请勾选同意协议");
			}
		});
	}
}